package com.kits.store.ui

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Color
import android.os.Bundle
import android.os.Environment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.kits.store.R
import com.kits.store.core.ExternalStorage
import kotlinx.android.synthetic.main.fragment_external_storage.*
import me.yokeyword.fragmentation.SupportFragment
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.OutputStream

class ExternalStorageFragment : SupportFragment(){

    companion object{
        fun newInstance(): ExternalStorageFragment{
            val args = Bundle()

            val fragment = ExternalStorageFragment()
            fragment.arguments = args
            return fragment
        }
    }


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_external_storage, container, false)
    }

    override fun onResume() {
        super.onResume()
        initView()
    }

    private fun initView(){
//        btnFile.setOnClickListener {
//            val files = context?.getExternalFilesDirs(Environment.MEDIA_MOUNTED)
//            files?.forEach {
//                println("path == ${it.absolutePath}")
//            }
//        }
        btnStandFile.setOnClickListener {
            val file = ExternalStorage.standFile(requireContext(),Environment.DIRECTORY_DCIM,"test","myTest123")
            println("path == ${file?.absolutePath}")
            val outputStream = FileOutputStream(file)
            outputStream.write("向标准目录写入文件，看一下是否可以创建子文件夹\n".toByteArray())
            outputStream.close()

        }

        btnReadStandFile.setOnClickListener {
            val file = ExternalStorage.standFile(requireContext(),Environment.DIRECTORY_DCIM,"test","myTest123")
            println("path == ${file?.absolutePath}")
            val inputStream = FileInputStream(file)
            val bytes = ByteArray(inputStream.available());
            inputStream.read(bytes)
            val strBuf = String(bytes)
            println("strBuf == $strBuf")
        }

        btnNotStandFile.setOnClickListener {
            val file = ExternalStorage.standFile(requireContext(),"myTest","test","myNotStandTest123")
            println("path == ${file?.absolutePath}")
            val outputStream = FileOutputStream(file)
            outputStream.write("向非标准目录写入文件，看一下是否可以创建子文件夹\n".toByteArray())
            outputStream.close()
        }

        btnReadNotStandFile.setOnClickListener {
            val file = ExternalStorage.standFile(requireContext(),"myTest","test","myNotStandTest123")
            println("path == ${file?.absolutePath}")
            val inputStream = FileInputStream(file)
            val bytes = ByteArray(inputStream.available());
            inputStream.read(bytes)
            val strBuf = String(bytes)
            println("strBuf == $strBuf")
        }


        btnCacheFile.setOnClickListener {
            val file = ExternalStorage.cacheFile(requireContext(),"test","myCache123")
            println("path == ${file?.absolutePath}")
            val outputStream = FileOutputStream(file)
            outputStream.write("向缓存目录写入文件，看一下是否可以创建子文件夹\n".toByteArray())
            outputStream.close()

//            val testFile = context?.externalCacheDir
//            println("testFile == ${testFile?.absolutePath}")
//            val outputStream = FileOutputStream(File(testFile,"jjj.txt"))
//            outputStream.write("直接向缓存目录写入文件，看一下是否可以创建子文件夹\n".toByteArray())
//            outputStream.close()
        }

        btnReadCacheFile.setOnClickListener {
            val file = ExternalStorage.cacheFile(requireContext(),"test","myCache123")
            println("path == ${file?.absolutePath}")
            val inputStream = FileInputStream(file)
            val bytes = ByteArray(inputStream.available());
            inputStream.read(bytes)
            val strBuf = String(bytes)
            println("strBuf == $strBuf")
        }

        btnImageFile.setOnClickListener {
            val imgFile = ExternalStorage.standFile(requireContext(),Environment.DIRECTORY_PICTURES,"test","simple1.png")

            val outputStream = FileOutputStream(imgFile)
            val bitmap = Bitmap.createBitmap(64, 64, Bitmap.Config.ARGB_8888)
            val tmpCanvas = Canvas()
            tmpCanvas.setBitmap(bitmap)
            tmpCanvas.drawColor(Color.GREEN)
            tmpCanvas.save()
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
            outputStream.close()
            bitmap.recycle()

        }


        btnMediaFiles.setOnClickListener{
            val files = requireContext().externalMediaDirs
            files.forEach {
                println("it == $it")
            }
        }

    }
}